package com.powernode.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.domain.IndexImg;
import com.powernode.model.Result;
import com.powernode.service.IndexImgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 轮播图业务管理控制层
 */
@Api(tags = "轮播图业务管理")
@RestController
@RequestMapping("/admin/indexImg")
public class IndexImgController {

    @Autowired
    private IndexImgService indexImgService;


    /**
     * 多条件分页查询轮播图
     * @param current 当前页码
     * @param size 每页显示数量
     * @param status 状态
     * @return
     */
    @ApiOperation("多条件分页查询轮播图")
    @GetMapping("page")
    @PreAuthorize("hasAuthority('admin:indexImg:page')")
    public Result<Page<IndexImg>> loadIndexImgPage(@RequestParam Long current,
                                               @RequestParam Long size,
                                               @RequestParam(required = false) Integer status) {
        Page<IndexImg> indexImgPage = new Page<>(current, size);
        indexImgPage = indexImgService.page(indexImgPage, new LambdaQueryWrapper<IndexImg>()
                .eq(ObjectUtil.isNotNull(status), IndexImg::getStatus, status));
        return Result.success(indexImgPage);
    }


    /**
     * 新增轮播图
     * @param indexImg 新增的参数
     * @return
     */
    @ApiOperation("新增轮播图")
    @PostMapping
    @PreAuthorize("hasAuthority('admin:indexImg:save')")
    public Result<String> saveIndexImg(@RequestBody IndexImg indexImg) {
        boolean saved = indexImgService.saveIndexImg(indexImg);
        return Result.handle(saved);
    }



    /**
     * 根据标识查询轮播图信息
     * @param imgId 标识
     * @return
     */
    @ApiOperation("根据标识查询轮播图信息")
    @GetMapping("info/{imgId}")
    @PreAuthorize("hasAuthority('admin:indexImg:info')")
    public Result<IndexImg> loadIndexImgInfo(@PathVariable Long imgId) {
        return Result.success(indexImgService.queryIndexImgInfoById(imgId));
    }


    /**
     * 修改轮播图内容
     * @param indexImg 修改的参数
     * @return
     */
    @ApiOperation("修改轮播图内容")
    @PutMapping
    @PreAuthorize("hasAuthority('admin:indexImg:update')")
    public Result<String> modifyIndexImg(@RequestBody IndexImg indexImg) {
        boolean modified = indexImgService.modifyIndexImg(indexImg);
        return Result.handle(modified);
    }


    /**
     * 批量删除轮播图
     * @param imgIds 标识集合
     * @return
     */
    @ApiOperation("批量删除轮播图")
    @DeleteMapping
    @PreAuthorize("hasAuthority('admin:indexImg:delete')")
    public Result<String> removeIndexImg(@RequestBody List<Long> imgIds) {
        Boolean removed = indexImgService.removeIndexImgByIds(imgIds);
        return Result.handle(removed);

    }
}
